﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class ONT_GPON_Ser
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();
        private LAP_THIETBI LapTbLastFial { get; set; }
        private List<int> _listParentIdJson_GianDo { get; set; }

        public ONT_GPON_Ser()
        {
            LapTbLastFial = new LAP_THIETBI();
            _listParentIdJson_GianDo = new List<int>();
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public bool addorUpdate(ONT_GPON entity)
        {
            try
            {
                if (entity.ONTID == 0) dbContext.ONT_GPON.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<ONT_GPON> getlistByTB(int tbId)
        {
            try
            {
                if (tbId == 0)
                {
                    //var listEntityGPON = dbContext.ONT_GPON.OrderBy(p => p.ONU_ID).ToList();
                    //return listEntityGPON;
                    List<ONT_GPON> _listnull = new List<ONT_GPON>();
                    return _listnull;
                }
                else
                {
                    var inArray_ = GetArrayTreeChilDNTB(tbId);
                    var listEntityGPON = dbContext.ONT_GPON.Where(p => inArray_.Contains(p.SPLITTER16.Value)).OrderBy(p => p.ONU_ID).ToList();
                    return listEntityGPON;
                }
            }
            catch (Exception e)
            {
                List<ONT_GPON> _list = new List<ONT_GPON>();
                return _list;
            }
        }

        public ONT_GPON GetById(int ontId)
        {
            try
            {
                return dbContext.ONT_GPON.Where(p => p.ONTID == ontId).First();
            }
            catch (Exception e)
            {
                ONT_GPON _return = new ONT_GPON();
                return _return;
            }
        }

        public long[] GetArrayTreeChilDNTB(int tbId)
        {
            try
            {
                List<long> _listReturn = new List<long>();
                var _listEn = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == tbId).ToList();
                if (_listEn != null)
                {
                    foreach (var item in _listEn)
                    {

                        _listReturn.Add(item.THIETBIID);
                        AddTbInFindChildInTree(_listReturn, item.THIETBIID);
                    }
                }

                _listReturn.Add(tbId);

                return _listReturn.ToArray();
            }
            catch (Exception e)
            {
                List<long> _listReturnNone = new List<long>();
                _listReturnNone.Add(tbId);
                return _listReturnNone.ToArray();
            }
        }

        public long[] AddTbInFindChildInTree(List<long> _list, int TbId)
        {
            try
            {
                var _listEn = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == TbId).ToList();
                if (_listEn != null)
                {
                    foreach (var item in _listEn)
                    {
                        _list.Add(item.THIETBIID);
                        AddTbInFindChildInTree(_list, item.THIETBIID);
                    }
                }

                return _list.ToArray();
            }
            catch (Exception ex)
            {
                List<long> _listReturnNone = new List<long>();
                _listReturnNone.Add(TbId);
                return _listReturnNone.ToArray();
            }
        }


        public List<THUEBAO_GPON> listTHUEBAOGPONBYONT(long ontId)
        {
            try
            {
                return dbContext.THUEBAO_GPON.Where(p => p.ONT_ID == ontId).ToList();
            }
            catch (Exception e)
            {
                List<THUEBAO_GPON> _return = new List<THUEBAO_GPON>();
                return _return;
            }
        }

        public List<ONT_GPON> listONTGPONBYArrayTB(long[] ontId)
        {
            try
            {
                List<long> TBId = new List<long>();
                for (int i = 0; i < ontId.Length; i++)
                {
                    TBId.AddRange(ListArrayChildTByParentTbid(ontId[i]));
                }

                var arrayIntTb = TBId.ToArray();

                return dbContext.ONT_GPON.Where(p => arrayIntTb.Contains(p.SPLITTER16.Value)).ToList();
            }
            catch (Exception e)
            {
                List<ONT_GPON> _return = new List<ONT_GPON>();
                return _return;
            }
        }

        public THUEBAO_GPON GetThueBaoGPonById(long Id)
        {
            try
            {
                return dbContext.THUEBAO_GPON.Where(p => p.ID == Id).First();
            }
            catch (Exception e)
            {
                THUEBAO_GPON _return = new THUEBAO_GPON();
                return _return;
            }
        }

        public bool Dele_THUEBAO_GPON(int Id)
        {
            try
            {
                var rnt = GetThueBaoGPonById(Id);
                dbContext.THUEBAO_GPON.Remove(rnt);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool AddUpdate_THUEBAO_GPON(THUEBAO_GPON entity)
        {
            try
            {
                if (entity.ID == 0) dbContext.THUEBAO_GPON.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e) { return false; }
        }

        public List<THUEBAO_CSS> getListThueBaoCSSGPON(string mavt, string[] donvi, string[] toql)
        {
            try
            {
                return dbContext.THUEBAO_CSS.Where(p => p.MANG_VT == mavt && donvi.Contains(p.MA_DVQLTB) && toql.Contains(p.MA_TRAM)).ToList();
            }
            catch (Exception e)
            {
                List<THUEBAO_CSS> _return = new List<THUEBAO_CSS>();
                return _return;
            }
        }

        public THUEBAO_CSS GetThueBaoCssOntById(long Id)
        {
            try
            {
                return dbContext.THUEBAO_CSS.Where(p => p.THUEBAO_ID == Id).First();
            }
            catch (Exception e)
            {
                THUEBAO_CSS _return = new THUEBAO_CSS();
                return _return;
            }
        }

        //public List<GianDoEntity> viewGianDoByDauNoTb(int daunoiTb)
        //{
        //    try
        //    {
        //        List<GianDoEntity> _returnJson = new List<GianDoEntity>();

        //        var LapTbParent = dbContext.DAUNOI_THIETBI.Where(p => p.THIETBIID == daunoiTb).First();
        //        var VNPTSITE = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbParent.SITEID).First();
        //        var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE.LOAISITE).First();

        //        GianDoEntity _ebtityParent = new GianDoEntity();
        //        _ebtityParent.id = LapTbParent.THIETBIID;
        //        _ebtityParent.name = VNPTSITE.TEN;
        //        _ebtityParent.Images = TBId.MARKER;
        //        _ebtityParent.PortSo = ""; // PortOutParent.PORT_SO.ToString();
        //        if (LapTbParent.THIETBIID == daunoiTb) { _ebtityParent.Color = "blue"; }
        //        else { _ebtityParent.Color = "black"; }

        //        AddLapTB_Child_GianDo(_ebtityParent.children, _ebtityParent.intArrayItemChild, LapTbParent.THIETBIID);

        //        _returnJson.Add(_ebtityParent);



        //        return _returnJson;
        //    }
        //    catch (Exception e)
        //    {
        //        List<GianDoEntity> _return = new List<GianDoEntity>();
        //        return _return;
        //    }
        //}

        //public void AddLapTB_Child_GianDo(List<GianDoEntity> gianDoJosn, List<int> arrayIntChild, int parentId)
        //{
        //    var entityDauNoiTb = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == parentId).ToList();

            

        //    foreach (var item in entityDauNoiTb)
        //    {
        //        var VNPTSITE = dbContext.VNPT_SITE.Where(p => p.SITEID == item.SITEID).First();
        //        var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE.LOAISITE).First();

        //        GianDoEntity _ebtityParent = new GianDoEntity();
        //        _ebtityParent.id = item.THIETBIID;
        //        _ebtityParent.name = VNPTSITE.TEN;
        //        _ebtityParent.Images = TBId.MARKER;
        //        _ebtityParent.PortSo = item.PORT_SO.ToString(); // PortOutParent.PORT_SO.ToString();
        //        if (item.THIETBIID == parentId) { _ebtityParent.Color = "blue"; }
        //        else { _ebtityParent.Color = "black"; }

        //        if (item.DAUVAO != null)
        //        {
        //            AddLapTB_Child_GianDo(_ebtityParent.children, _ebtityParent.intArrayItemChild, item.THIETBIID);

        //        }
                
        //        gianDoJosn.Add(_ebtityParent);
        //        arrayIntChild.Add(item.THIETBIID);

        //    }
        //}


        public List<long> ListArrayChildTByParentTbid(long tbid)
        {
            List<long> ListReturn = new List<long>();


            var Listretview = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == tbid).ToList();
            foreach(var item in Listretview)
            {
                if (item.DAUVAO != null)
                {
                    ListArrayChildTByParentTbid_child(ListReturn, item.THIETBIID);
                }

                ListReturn.Add(item.THIETBIID);
            }

            ListReturn.Add(tbid);

            return ListReturn;
        }


        public void ListArrayChildTByParentTbid_child(List<long> add, long tbid)
        {
            var Listretview = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == tbid).ToList();
            foreach (var item in Listretview)
            {
                if (item.DAUVAO != null)
                {
                    ListArrayChildTByParentTbid_child(add, item.THIETBIID);
                }

                add.Add(item.THIETBIID);
            }
        }


        public List<_listJsontoTree> getListTreeFromArrayTB(long[] ontId)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                for (int i = 0; i < ontId.Length; i++)
                {
                    long idTbid = ontId[i];
                    var daunoiTb = dbContext.DAUNOI_THIETBI.Where(p => p.THIETBIID == idTbid).First();
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = idTbid.ToString();
                    itemMVT.text = daunoiTb.TEN;
                    itemMVT.type = "dntb";
                    itemMVT.Idparent = idTbid.ToString();

                    getListTreeFromArrayTB_child(itemMVT.children, ontId[i]);


                    _listRetuen.Add(itemMVT);

                }

                

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }


        public void getListTreeFromArrayTB_child(List<_listJsontoTree> listadd, long tbid)
        {
            var Listretview = dbContext.DAUNOI_THIETBI.Where(p => p.DAUVAO == tbid).ToList();
            foreach (var item in Listretview)
            {
                var daunoiTb = dbContext.DAUNOI_THIETBI.Where(p => p.THIETBIID == item.THIETBIID).First();
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.THIETBIID.ToString();
                    itemMVT.text = daunoiTb.TEN;
                    itemMVT.type = "dntb";
                    itemMVT.Idparent = item.THIETBIID.ToString();
                    
                    if (item.DAUVAO != null)
                    {
                        getListTreeFromArrayTB_child(itemMVT.children, item.THIETBIID);
                    }

                listadd.Add(itemMVT);
            }
        }

        //Giản Đồ
        public List<GianDoEntity> viewGianDoByLapTb(int laptb)
        {
            try
            {
                List<GianDoEntity> _returnJson = new List<GianDoEntity>();

                var LapTbParent = FindLapTBParentNode_GianDo(laptb);

                var VNPTSITE = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbParent.SITEID).First();
                var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE.LOAISITE).First();


                if (LapTbParent.ID != laptb)
                {

                    var VNPTSITE_lastFinnal = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbLastFial.SITEID).First();
                    var TbLastFinnal = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE_lastFinnal.LOAISITE).First();

                    var PortOutParent = dbContext.PORT.Where(p => p.THIETBIID == LapTbParent.ID && p.TB_OUT == LapTbLastFial.ID).First();

                    GianDoEntity _ebtityParent = new GianDoEntity();
                    _ebtityParent.id = LapTbParent.ID;
                    _ebtityParent.name = VNPTSITE.TEN;
                    _ebtityParent.Images = TBId.MARKER;
                    _ebtityParent.PortSo = ""; // PortOutParent.PORT_SO.ToString();
                    if (LapTbParent.ID == laptb) { _ebtityParent.Color = "blue"; }
                    else { _ebtityParent.Color = "black"; }
                    _ebtityParent.thongSoPort = thongsoPortBycauhinhTb(LapTbParent.ID);
                    _ebtityParent.intArrayItemChild.Add(LapTbParent.ID);


                    GianDoEntity _ebtityParentLastChild = new GianDoEntity();
                    _ebtityParentLastChild.id = LapTbLastFial.ID;
                    _ebtityParentLastChild.name = VNPTSITE_lastFinnal.TEN;
                    _ebtityParentLastChild.Images = TbLastFinnal.MARKER;
                    _ebtityParentLastChild.PortSo = "Port: " + PortOutParent.PORT_SO.ToString();
                    if (LapTbLastFial.ID == laptb) { _ebtityParentLastChild.Color = "blue"; }
                    else { _ebtityParentLastChild.Color = "black"; }
                    _ebtityParentLastChild.thongSoPort = thongsoPortBycauhinhTb(LapTbLastFial.ID);

                    AddLapTB_Child_GianDo(_ebtityParentLastChild.children, _ebtityParentLastChild.intArrayItemChild, LapTbLastFial.ID, laptb);

                    _ebtityParent.children.Add(_ebtityParentLastChild);

                    _returnJson.Add(_ebtityParent);

                }
                else
                {
                    GianDoEntity _ebtityParent = new GianDoEntity();
                    _ebtityParent.id = LapTbParent.ID;
                    _ebtityParent.name = VNPTSITE.TEN;
                    _ebtityParent.Images = TBId.MARKER;
                    _ebtityParent.PortSo = ""; // PortOutParent.PORT_SO.ToString();
                    if (LapTbParent.ID == laptb) { _ebtityParent.Color = "blue"; }
                    else { _ebtityParent.Color = "black"; }
                    _ebtityParent.intArrayItemChild.Add(LapTbParent.ID);
                    _ebtityParent.thongSoPort = thongsoPortBycauhinhTb(LapTbParent.ID);

                    AddLapTB_Child_GianDo(_ebtityParent.children, _ebtityParent.intArrayItemChild, LapTbParent.ID, laptb);

                    _returnJson.Add(_ebtityParent);

                }


                return _returnJson;
            }
            catch (Exception e)
            {
                List<GianDoEntity> _return = new List<GianDoEntity>();
                return _return;
            }
        }

        public string thongsoPortBycauhinhTb(int laptbId)
        {
            string _returns = "";

            var ListCauHinhByTB = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == laptbId).ToList();
            if (ListCauHinhByTB != null)
            {
                if (ListCauHinhByTB.Count == 1)
                {
                    var CauHinhEntityFirst = ListCauHinhByTB.First();

                    var ListPort = dbContext.PORT.Where(p => p.CAUHINHID == CauHinhEntityFirst.ID).ToList();

                    //int PortCSd = ListPort.Where(p => p.TRANGTHAI != 2).Count();
                    int PortDSd = ListPort.Where(p => p.TRANGTHAI == 2).Count();
                    //int PortLP = ListPort.Where(p => p.TRANGTHAI == 3).Count();
                    //int PortKP = ListPort.Where(p => p.TRANGTHAI == 4).Count();

                    _returns = "Sử dụng: " + PortDSd.ToString() + "/" + ListPort.Count();

                }
                else
                {

                }
            }


            return _returns;
        }

        public void AddLapTB_Child_GianDo(List<GianDoEntity> gianDoJosn, List<int> arrayIntChild, int parentId, int lapTbCurrent)
        {
            var entitylapTb = dbContext.LAP_THIETBI.Where(p => p.ID == parentId).First();
            var entityCauHinhTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == entitylapTb.ID).First();
            var enityListPort = dbContext.PORT.Where(p => p.CAUHINHID == entityCauHinhTb.ID).ToList();
            foreach (var item in enityListPort)
            {
                if (item.TB_OUT != null)
                {
                    GianDoEntity _ebtityParent = new GianDoEntity();
                    var LapTbId = dbContext.LAP_THIETBI.Where(p => p.ID == item.TB_OUT).First();

                    var VNPTSITEitem = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbId.SITEID).First();
                    var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITEitem.LOAISITE).First();

                    var PortOutParent = dbContext.PORT.Where(p => p.THIETBIID == parentId && p.TB_OUT == LapTbId.ID).First();

                    _ebtityParent.id = LapTbId.ID;
                    _ebtityParent.name = VNPTSITEitem.TEN;
                    _ebtityParent.Images = TBId.MARKER;
                    _ebtityParent.PortSo = "Port: " + PortOutParent.PORT_SO.ToString();
                    if (LapTbId.ID == lapTbCurrent) { _ebtityParent.Color = "blue"; }
                    else { _ebtityParent.Color = "black"; }
                    _ebtityParent.thongSoPort = thongsoPortBycauhinhTb(LapTbId.ID);

                    AddLapTB_Child_GianDo(_ebtityParent.children, _ebtityParent.intArrayItemChild, LapTbId.ID, lapTbCurrent);

                    gianDoJosn.Add(_ebtityParent);
                    arrayIntChild.Add(item.THIETBIID.Value);

                }

            }
        }

        public LAP_THIETBI FindLapTBParentNode_GianDo(int laptbId)
        {
            //LAP_THIETBI _returnJson = new LAP_THIETBI();
            var entityLBTBID = dbContext.LAP_THIETBI.Where(p => p.ID == laptbId).First();
            var entityCauHinhTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == entityLBTBID.ID).First();
            var enityPort = dbContext.PORT.Where(p => p.CAUHINHID == entityCauHinhTb.ID).First();

            if (enityPort.TB_IN != null)
            {
                LapTbLastFial = entityLBTBID;
                return FindLapTBParentNode_GianDo(enityPort.TB_IN.Value);
            }
            else
            {
                return dbContext.LAP_THIETBI.Where(p => p.ID == enityPort.THIETBIID).First();

            }

        }



    }
}
